"""
DRF中异常处理
"""

from rest_framework.views import exception_handler
from rest_framework.response import Response
from rest_framework import status


def custom_exception_handle(exc, context):
    # 让drf做异常判断
    response = exception_handler(exc, context)

    # 判断 response是否为None
    if response is None:

        # 判断是哪种异常
        if isinstance(exc, ZeroDivisionError):
            # 返回
            return Response({'error':"除数不能为0!"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        if isinstance(exc, Exception):
            return Response({'error': "后台未知的错误!"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)


    return response